package com.mes.web.controller.lc;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mes.entity.lc.CheckStock;
import com.mes.entity.lc.CheckStockDetail;
import com.mes.entity.lc.MaterialStock;
import com.mes.entity.lc.ProductStock;
import com.mes.service.lc.CheckStockService;
import com.mes.service.lc.MaterialStockService;
import com.mes.service.lc.ProductStockService;
import com.mes.utils.ResponseUtil;
import com.mes.utils.StringUtil;

/**
 * 库存盘点实体类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/checkstock")
public class CheckStockController {

	@Autowired
	CheckStockService	checkStockService; //盘点单
	@Autowired
	MaterialStockService	materialStockService;//物料库存
	@Autowired
	ProductStockService		productStockService;//产品库存
	
	/**
	 * 盘点单查询
	 * @param stockfy
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception
	 */
	@RequestMapping("/selectfycs")
	public @ResponseBody void selectfycx(
			CheckStock checkStock,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			checkStock.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			checkStock.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(checkStock.getPname())) {
				checkStock.setPname(null);
			}//盘点单号
			if (StringUtil.isEmpty(checkStock.getPtype())) {
				checkStock.setPtype(null);
			}//盘点类型
			if (StringUtil.isEmpty(checkStock.getPbody())) {
				checkStock.setPbody(null);
			}//盘点人
	
			System.err.println(checkStock.getPtype() + "------------盘点类型");
			
			// 物料
			List<CheckStock> cslist = checkStockService.selectfycs(checkStock);
			int a = checkStockService.selectfycscont(checkStock);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(cslist, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 盘点单添加
	 * @throws Exception 
	 */
	@RequestMapping("/addpdd")
	public void addpdd(CheckStock checkStock , HttpServletResponse response, HttpServletRequest request) throws Exception{
		
		
			//单号
			Date now = new Date();
	 		SimpleDateFormat dateFormatt = new SimpleDateFormat("yyyyMMddHHmmss");
	 		String nowtimee = dateFormatt.format(now); 
	 		checkStock.setPname("PD" + nowtimee);
			Integer act = checkStockService.addpdd(checkStock);
			
			//在新建立盘点单的时候，自动盘点对应库存详情
			String isn = checkStock.getPtype();
			if("物料".equals(isn)){
				//盘点物料
			    List<MaterialStock> materst = materialStockService.selectMaterialStock();
			    for(MaterialStock mater : materst){
			    	CheckStockDetail csd = new CheckStockDetail();
				    Long acter = act.longValue();
					csd.setPidd(acter);//盘点单主键
					csd.setPdname(mater.getMateriel().getName());//品名
					csd.setPdnumb(mater.getMateriel().getNumber());//品编
					csd.setPdssck(mater.getWssck());//库区
					csd.setPdcolour(mater.getMateriel().getColor());//颜色
					csd.setPnum(mater.getWnum());//库存数量
					csd.setPdnum("0");//盘点数量
					checkStockService.addCheckStockDetail(csd);
			    }
			}else if("产品".equals(isn)){
				//盘点产品
			    List<ProductStock> product = productStockService.selectProductStock();
			    for(ProductStock prod : product){
			    	CheckStockDetail csd = new CheckStockDetail();
				    Long acter = act.longValue();
					csd.setPidd(acter);//盘点单主键
					csd.setPdname(prod.getProduct().getName());//品名
					csd.setPdnumb(prod.getProduct().getNum());//品编
					csd.setPdssck(prod.getSsck());//库区
					csd.setPdcolour(prod.getProduct().getColor());//颜色
					csd.setPnum(prod.getNum());//库存数量
					csd.setPdnum("0");//盘点数量
					checkStockService.addCheckStockDetail(csd);
			    }
			}
		
		 if(act >0){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", true);
			ResponseUtil.write(response, jsonObject);
		 }else{
			 JSONObject jsonObject = new JSONObject();
			 jsonObject.put("success", false);
			 ResponseUtil.write(response, jsonObject);
		 }
	}
	
	
	/**
	 * 盘点单删除
	 * @throws Exception 
	 */
	@RequestMapping("/deletepdd")
	public void deletepdd(Long ids[] , HttpServletResponse response, HttpServletRequest request) throws Exception{
		
			int act = 0 ;
			CheckStock checkStock = new CheckStock();
			for(int i = 0 ; i <ids.length ; i++){
				checkStock.setPid(ids[i]);
				checkStockService.deletepdd(checkStock);
				act++;
			}
		
		 if(act >0){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", true);
			ResponseUtil.write(response, jsonObject);
		 }else{
			 JSONObject jsonObject = new JSONObject();
			 jsonObject.put("success", false);
			 ResponseUtil.write(response, jsonObject);
		 }
	}
	
	/**
	 * 盘点单详情分页查询
	 * @param checkStock
	 * @param response
	 * @param limit
	 * @param offset
	 * @throws Exception
	 */
	@RequestMapping("/selectfycsc")
	public @ResponseBody void selectfycsc(
			CheckStockDetail  checkStockDetail,HttpServletResponse response,
			@RequestParam(value = "limit", required = false) String limit,
			@RequestParam(value = "offset", required = false) String offset)
			throws Exception {
		
			checkStockDetail.setPage(Integer.parseInt(offset) / Integer.parseInt(limit));
			
			// 每页条数
			checkStockDetail.setPageSize(Integer.parseInt(limit));
	
			if (StringUtil.isEmpty(checkStockDetail.getPdnumb())) {
				checkStockDetail.setPdnumb(null);
			}//品编
			if (StringUtil.isEmpty(checkStockDetail.getPdname())) {
				checkStockDetail.setPdname(null);
			}//品名
			
			// 盘点单详情
			List<CheckStockDetail> csdeta = checkStockService.selectfycsc(checkStockDetail);
			Integer a = checkStockService.selectfycsccont(checkStockDetail);
			JsonConfig config = new JsonConfig();
			JSONObject jsonObject = new JSONObject();
			JSONArray jsonArray = JSONArray.fromObject(csdeta, config);
			jsonObject.put("rows", jsonArray);
			jsonObject.put("total", a);
			ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 盘点单详情修改
	 * @throws Exception 
	 */
	@RequestMapping("/updatexiangq")
	public void updatexiangq(CheckStockDetail checkStockDetail, HttpServletResponse response, HttpServletRequest request) throws Exception{
		 
		checkStockService.updatexiangq(checkStockDetail);
		int act = checkStockDetail.getActter();
		if(act > 0){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", true);
			ResponseUtil.write(response, jsonObject);
		}else{
			 JSONObject jsonObject = new JSONObject();
			 jsonObject.put("success", false);
			 ResponseUtil.write(response, jsonObject);
		}
	}
	
	/**
	 * 根据主键查询盘点单
	 * @throws Exception 
	 */
	@RequestMapping("/selectPanid")
	public void selectPanid(Long id, HttpServletResponse response, HttpServletRequest request) throws Exception{
		CheckStock cs = checkStockService.selectPanid(id);
		new JSONObject();
		JSONObject jsonObject = JSONObject.fromObject(cs);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 盘点单删除
	 * @throws Exception 
	 */
	@RequestMapping("/updatepdd")
	public void updatepdd(CheckStock checkStock, HttpServletResponse response, HttpServletRequest request) throws Exception{
		
		checkStockService.updatepdd(checkStock);
		
		int act = checkStock.getActter();
		
		 if(act >0){
			 
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("success", true);
			ResponseUtil.write(response, jsonObject);
			
		 }else{
			 JSONObject jsonObject = new JSONObject();
			 jsonObject.put("success", false);
			 ResponseUtil.write(response, jsonObject);
		 }
	}
	
	
}
